<template>
	<view class="pay">
		<!-- 地址 -->
		<view class="pay_address flex-row-between" @click="goNext('/pagesA/my/address?type=confirm',false)">
			<view v-if="addressobj.address_id">
				<view class="address_box">
					<text class="font-1e1e1e font15 address_name font-b">{{addressobj.name}}</text>
					<text class="font-1e1e1e font15 font-b">{{addressobj.phone}}</text>
				</view>
				<view class="font-646464 font12">
					{{addressobj.province_name}}{{addressobj.city_name}}{{addressobj.region_name}}{{addressobj.detail}}
				</view>
			</view>
			<view v-else class="font-1e1e1e font15">暂无收货地址,点击添加地址</view>
		</view>
		<!-- 数据 -->
		<view class="order_info order_info_m">
			<view class="font-333333 font15 font-b margin-30">{{obj.store.name}}</view>
			<view class="order_box" v-for="(item,index) in obj.goods_list" :key="index">
				<view class="flex-row-between">
					<image :src="imgFullUrl+item.goods_img" mode="" class="orderi_img"></image>
					<view class="flex-column-between orderi_box">
						<view class="font-333333 font13">{{item.goods_name}}</view>
						<view class="orderi_norms flex-row-start font-666666 font12">
							<text>规格及用法:</text>
							<view>{{item.spec_name}}{{item.frequencys}}{{item.consumptions}}</view>
						</view>
						<view class="font14 flex-row-between">
							<text class="font-ff3c37">￥{{item.goods_price}}</text>
							<text class="font-333333">x {{item.number}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 底部菜单 -->
		<view class="pay_menu flex-row-between">
			<view>
				<view class="font-b flex-row-start">
					<view class="font13 font-1e1e1e width_76">合计:</view>
					<text class="font-ff3c37 font15">
						<text class="font11">￥</text>{{obj.pay_price}}
					</text>
				</view>
			</view>
			<view class="menu_submit flex-row-center font-ffffff font13" @click="OnSubmit">提交</view>
		</view>
		<view class="pay_menus"></view>
	</view>
</template>


<script>
	export default {
		data() {
			return {
				obj: {}, //数据
				imgFullUrl: this.$store.state.http_img, //图片地址
				id: null, //ID
				addressobj: {}, //默认地址
			};
		},
		onLoad: function(options) {
			this.id = options.id
			this.OnOrder()
			this.OngetAddress()
			// 开启监听选择收货地址事件，如果有值就关闭默认地址
			uni.$on('choosePath', (res) => {
				this.addressobj = res
			})
		},
		onUnload() {
			// 关闭监听选择收货地址事件
			uni.$off('choosePath')
		},
		methods: {
			//详情1
			OnOrder() {
				this.$Http({
					url: 'mydetailsPrescription',
					data: {
						doctor_prescription_id: this.id
					},
				}).then(res => {
					if (res.code == 1) {
						res.data.createtime = this.$base.formatTime(new Date(res.data.createtime *
							1000), "yyyy/MM/dd hh:mm:ss")
						this.obj = res.data
					}
				});
			},
			//地址列表
			OngetAddress() {
				this.addressobj = {}
				this.$Http({
					url: 'listAddress',
					data: {},
				}).then(res => {
					if (res.code == 1) {
						this.addressList = res.data
						res.data.forEach(item => {
							if (item.isdefault == 1) {
								this.addressobj = item
							}
						})
					}
				});
			},
			//跳转
			goNext(url) {
				uni.navigateTo({
					url: url
				});
			},
			//提交
			OnSubmit() {
				if (!this.addressobj.address_id) {
					uni.showToast({
						icon: 'none',
						duration: 2000,
						title: '请先添加收货地址'
					});
					return
				}
				this.$Http({
					url: 'payPrescription',
					data: {
						doctor_prescription_id: this.id,
						address_id:this.addressobj.address_id
					},
					type: 'POST',
					show:true,
				}).then(res => {
					if (res.code == 1) {
						this.Onpay(res.data)
					}
				});
				// console.log(data, '查看')
			},
			//支付
			Onpay(e) {
				var that = this
				uni.requestPayment({
					provider: 'wxpay',
					timeStamp: e.timeStamp,
					nonceStr: e.nonceStr,
					package: e.pacKage,
					signType: e.signType,
					paySign: e.paySign,
					success: function(res) {
						//去列表
						uni.redirectTo({
							url: '/pagesA/drug/list',
						})
					},
					fail: function(err) {
						uni.showModal({
							title: '支付提示',
							content: '支付失败,请联系客服人员!',
							confirmText: '我知道了',
							confirmColor: '#6ccbb6',
							showCancel: false,
							success(res) {
								if (res.confirm) {
									uni.redirectTo({
										url: '/pagesA/drug/list',
									})
								}
							}
						})
					}
				});

			}
		}
	}
</script>

<style lang="less">
	@import url("style.less");
</style>
